#pragma once
#include <string>
#include <iostream>
#include <vector>

#include "Common.hpp"

class Serialize
{
public:
    std::pair<std::string, std::string> Decode_user_pass(std::string &inbuffer)
    {
        std::vector<std::string> parts;
        size_t start = 0;
        size_t end = inbuffer.find(SEP);

        while (end != std::string::npos)
        {
            parts.push_back(inbuffer.substr(start, end - start));
            start = end + SEP.length();
            end = inbuffer.find(SEP, start);
        }
        parts.push_back(inbuffer.substr(start));

        // 检查分割后的结果
        if (parts.size() >= 3)
        {
            // 假设 parts[0] 是 login_sign，parts[1] 是 user，parts[2] 是 password
            std::string user = parts[1];
            std::string password = parts[2];
            return std::make_pair(user, password);
        }
        else
        {
            return std::make_pair("", ""); // 返回空的用户名和密码
        }
    }
    std::string GetFirstSIGN(const std::string &inbuffer) // 拿出标志符
    {
        auto pos = inbuffer.find(SEP);
        if (pos == std::string::npos)
        {
            // 如果找不到分隔符，返回整个字符串
            return inbuffer;
        }
        return inbuffer.substr(0, pos);
    }
    MessageType GetFirstMsgSIGN(const std::string &inbuffer, std::string &remainingStr) // 拿出标志符和剩下的字符串
    {
        auto pos = inbuffer.find(SEP);
        if (pos == std::string::npos)
        {
            remainingStr.clear();
            return UNKNOWN_TYPE;
        }

        std::string typeStr = inbuffer.substr(0, pos);
        remainingStr = inbuffer.substr(pos + 1); // 获取剩下的字符串

        if (typeStr == "TEXT")
            return TEXT_TYPE;
        else if (typeStr == "IMAGE")
            return IMAGE_TYPE;
        else if (typeStr == "FILE")
            return FILE_TYPE;
        else if (typeStr == "SPEECH")
            return SPEECH_TYPE;
        else
            return UNKNOWN_TYPE;
    }
    void Decode_Text(const std::string &proto_str, std::string &chat_sign, std::string &username, std::string &message)
    {
        size_t first_sep = proto_str.find('|');                 // 查找第一个分隔符的位置
        size_t second_sep = proto_str.find('|', first_sep + 1); // 查找第二个分隔符的位置

        if (first_sep != std::string::npos && second_sep != std::string::npos)
        {
            chat_sign = proto_str.substr(0, first_sep);                             // 提取CHAT_SIGN部分
            username = proto_str.substr(first_sep + 1, second_sep - first_sep - 1); // 提取username部分
            message = proto_str.substr(second_sep + 1);                             // 提取message部分
        }
        else
        {
            // 如果没有找到分隔符，处理错误情况
            std::cerr << "Error: Invalid format" << std::endl;
        }
    }
    std::string GetRestPart(const std::string &inbuffer) // 拿出标志符
    {
        auto pos = inbuffer.find(SEP);
        if (pos == std::string::npos)
        {
            // 如果找不到分隔符，返回空字符串
            return "";
        }
        // 返回从分隔符之后到字符串末尾的子串
        return inbuffer.substr(pos + 1);
    }
    // std::pair<std::string, std::string> Decode_user_pass(std::string &inbuffer)
    std::string EncodeTextResponse(std::string&message,std::string&username)
    {
        const std::string Message(CHAT_SIGN+SEP+username+SEP+message);
        return Message;
    }
};
